import { defineAsyncComponent } from 'vue';
import { getTenantList, getDefaultAccount } from "@/api/login";
import { useUserStore } from '@/store/modules/user'
export enum RoleEnum {
  PLATFORM = 5, // 平台
  PROVIDER = 6, // 服务商
  OPERATOR = 7, // 运营商
  AGENT = 8, // 代理商
  MERCHANT = 9 // 商户
}

export interface RoleStrategy {
  getHomeComponent(): Promise<any>;
}

const sharedGetAsyncComponent = (path: string) => {
  return defineAsyncComponent(() => {
    return new Promise((resolve, reject) => {
      import(/* @vite-ignore */ path)
        .then((Component) => {
          resolve(Component as any);
        })
        .catch(() => {
          reject(null);
        });
    });
  });
};

export class DefaultHomeStrategy implements RoleStrategy {
  async getHomeComponent() {
    // return sharedGetAsyncComponent('./modules/DefaultHome.vue');
    return import.meta.glob('./modules/DefaultHome.vue');
  }
}

export class AgentStrategy implements RoleStrategy {
  async getHomeComponent() {
    // return sharedGetAsyncComponent('./modules/AgentStrategy.vue');
    return import.meta.glob('./modules/AgentStrategy.vue');
  }
}

export class ProviderStrategy implements RoleStrategy {
  async getHomeComponent() {
    // return sharedGetAsyncComponent('./modules/ProviderStrategy.vue');
    return import.meta.glob('./modules/ProviderStrategy.vue');

  }
}

export class PlatformStrategy implements RoleStrategy {
  async getHomeComponent() {
    // return sharedGetAsyncComponent('./modules/PlatformStrategy.vue');
    return import.meta.glob("./modules/PlatformStrategy.vue");
  }
}

export class OperateorStrategy implements RoleStrategy {
  async getHomeComponent() {
    // return sharedGetAsyncComponent('./modules/OperateorStrategy.vue');
    return import.meta.glob("./modules/OperateorStrategy.vue");

  }
}

export class MerchantStrategy implements RoleStrategy {
  async getHomeComponent() {
    // return sharedGetAsyncComponent('./modules/MerchantModule.vue');
    let res = await getDefaultAccount();
    let sydId = useUserStore().defaultProvider.sydId;
    if(!sydId){
      useUserStore().setDefaultProvider(res?.data || {})
    }
    return import.meta.glob('./modules/MerchantModule.vue');
  }
}

export class RoleStrategyFactory {
  static create(roleId: RoleEnum): RoleStrategy | null {
    switch (roleId) {
      case RoleEnum.PLATFORM:
        return new PlatformStrategy();
      case RoleEnum.PROVIDER:
        return new ProviderStrategy();
      case RoleEnum.OPERATOR:
        return new OperateorStrategy();
      case RoleEnum.AGENT:
        return new AgentStrategy();
      case RoleEnum.MERCHANT:
        return new MerchantStrategy();
      default:
        return new DefaultHomeStrategy();
    }
  }
}
